package qianfeng.com.meijiadaka.viewholder;

import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.daimajia.slider.library.SliderLayout;
import com.daimajia.slider.library.SliderTypes.BaseSliderView;
import com.daimajia.slider.library.SliderTypes.DefaultSliderView;

import java.util.ArrayList;
import java.util.List;

import cn.lemon.view.adapter.BaseViewHolder;
import qianfeng.com.meijiadaka.R;
import qianfeng.com.meijiadaka.bean.Home_info1;
import qianfeng.com.meijiadaka.bean.TypeBean;

/**
 * Created by linlongxin on 2016/8/23.
 */

public class ViewPagerHolder extends BaseViewHolder<TypeBean> {


    private ViewPager mViewPager;
    private LinearLayout mLayout;
    private List<View>mViews;
    private List<ImageView>mIV;
    private int mPage;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (mPage %5 == 0) {
                mViewPager.setCurrentItem(0, false);
            } else {
                mViewPager.setCurrentItem(mPage % 5);
            }
            mPage++;
        }
    };

    public ViewPagerHolder(ViewGroup parent) {
        super(parent, R.layout.home_viewpage);
        mViews=new ArrayList<>();
        mIV=new ArrayList<>();
    }
    @Override
    public void onInitializeView() {
        super.onInitializeView();
        mLayout = findViewById(R.id.ll);
        mViewPager=findViewById(R.id.vp);

    }

    @Override
    public void setData(TypeBean object) {
        super.setData(object);
        mIV.clear();
        mViews.clear();
        mLayout.removeAllViews();
        for (int i = 0; i < object.getAdsBeen().size(); i++) {
            Log.e("aa", object.getAdsBeen().get(i).getAdPicture());
            addQiu();
            ImageView imageView = new ImageView(itemView.getContext());
            Glide.with(itemView.getContext()).load(object.getAdsBeen().get(i).getAdPicture()).into(imageView);
            mIV.add(imageView);
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(itemView.getContext(),"点击轮播",Toast.LENGTH_SHORT).show();
                }
            });
        }
        MyViewPagerAdapter myViewPagerAdapter = new MyViewPagerAdapter();
        mViewPager.setAdapter(myViewPagerAdapter);
        new Thread() {
            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(3000);
                        mHandler.sendEmptyMessage(0);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                for (int i = 0; i < mViews.size(); i++) {
                    if (i == position) {
                        mViews.get(i).setSelected(true);
                    } else {
                        mViews.get(i).setSelected(false);
                    }
                }
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
            /*DefaultSliderView sliderView= new DefaultSliderView(itemView.getContext());
            sliderView.image(object.getAdsBeen().get(i).getAdPicture())
                    .setScaleType(BaseSliderView.ScaleType.Fit);
            sliderView.setOnSliderClickListener(new BaseSliderView.OnSliderClickListener() {
                @Override
                public void onSliderClick(BaseSliderView slider) {
                    Toast.makeText(itemView.getContext(),"nihao",Toast.LENGTH_SHORT).show();
                }
            });
            mLayout.addSlider(sliderView);
        }*/
        /*mLayout.setPresetTransformer(SliderLayout.Transformer.Accordion);
        // 设置默认指示器位置(默认指示器白色,位置在sliderlayout底部)
        mLayout.setPresetIndicator(SliderLayout.PresetIndicators.Center_Bottom);
        // 设置自定义指示器(位置自定义)
        //mLayout.setCustomIndicator((PagerIndicator) findViewById(R.id.custom_indicator));
        // 设置TextView自定义动画
        //mLayout.setCustomAnimation(new DescriptionAnimation());
        //mDemoSlider2.setCustomAnimation(new ChildAnimationExample()); // 多种效果，进入该类修改，参考效果github/daimajia/AndroidViewAnimations
        // 设置持续时间
        mLayout.setDuration(2000);*/
    }
    @Override
    public void onItemViewClick(TypeBean object) {
        super.onItemViewClick(object);
    }
    private void addQiu() {
        View indocator = new View(itemView.getContext());
        indocator.setBackgroundResource(R.drawable.icon_vp);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(10, 10);
        params.leftMargin = 5;
        params.gravity = Gravity.CENTER;
        indocator.setLayoutParams(params);
        mLayout.addView(indocator);
        mViews.add(indocator);
    }
    class MyViewPagerAdapter extends PagerAdapter {
        @Override
        public int getCount() {
            return mIV.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(mIV.get(position));
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View view = mIV.get(position);
            container.addView(view);
            return view;
        }
    }
}
